package fun.sboy.headacheMemo.controller;

import fun.sboy.headacheMemo.dto.ArticleSearch;
import fun.sboy.headacheMemo.entry.Article;
import fun.sboy.headacheMemo.service.ArticleService;
import fun.sboy.headacheMemo.util.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章 管理 搜索
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    @GetMapping("/hot")
    public ResultUtils getHot() {
        List<Article> list = articleService.selectHotArticle();
        return ResultUtils.success(list);
    }

    @PostMapping("/search")
    public ResultUtils search(@RequestBody ArticleSearch search) {
        List<Article> list = articleService.selectListArticle(search);
        return ResultUtils.success(list);
    }

    @GetMapping("/{aId}")
    public ResultUtils selectArticleById(@PathVariable("aId") String aId) {
        Article article = articleService.selectArticleById(aId);
        return ResultUtils.success(article);
    }

    @PostMapping()
    public ResultUtils insert(@RequestBody Article article) {
        articleService.insertArticle(article);
        return ResultUtils.success("添加成功！", null);
    }

    @PutMapping()
    public ResultUtils update(@RequestBody Article article) {
        articleService.updateArticle(article);
        return ResultUtils.success("更新成功！");
    }

    @DeleteMapping("/{aid}")
    public ResultUtils delete(@PathVariable("aid") String aid) {
        articleService.deleteArticle(aid);
        return ResultUtils.success("删除文章成功");
    }
}
